define(['jquery', 'domReady!', 'appLocalStorage', 'appConst',
        'model/global', 'model/export', 'appMessageBox'],
    function ($, domReady, appLocalStorage, appConst, global, exportM, msgBox) {

        var components = {};
        var data = {
            searchFields: {}
        };
        var that;
        return {
            init: function () {
                that = this;
                components = {
                    tb_exportHistory: $('#tb_exportHistory')
                };

                that.initHistoryTable();
                that.loadHistoryTable();
            },
            initHistoryTable: function () {
                components.tb_exportHistory.dataTable({
                    "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'><'col-sm-6 col-xs-12 hidden-xs'>r>" +
                    "t" +
                    "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
                    "pageLength": global.getPageSize(),
                    "ordering": false,
                    "paging": true,
                    "autoWidth": false,
                    // "stateSave": true,
                    "serverSide": true,
                    "language": appConst.table_language(),
                    "ordering": false,
                    "ajax": {
                        "url": 'empty',
                        "type": "POST",
                        "dataSrc": "body",
                        "traditional": true,
                        "data": function () {
                            return data.searchFields;
                        },
                        "error": function (e) {
                            if (e.status == 401) {
                                msgBox.sessionFailureBox();
                            }
                            if (e.status == 500) {
                                msgBox.serverFailureBox();
                            }
                            if (e.status == 404) {
                                msgBox.pathFailureBox();
                            }
                        }
                    },
                    //"processing": true,
                    //"deferRender": true,
                    "columns": [
                        {"data": null},
                        {"data": "projectName"},
                        {"data": "url"},
                        {"data": "createBy"},
                        {"data": "createTime"},
                        {"data": "status"},
                        {"data": "dataType"},
                        {"data": null}
                    ],
                    "columnDefs": [
                        {
                            "targets": [2],
                            "render": function (data, type, full) {
                                var operate = full.url;
                                if (full.status == "FINISHED") {
                                    operate = '<a href="' + full.url + '">' + operate + '</a>';
                                }
                                if (full.createBy == global.getCurrentUserName()) {
                                    return operate;
                                } else if (global.isAdmin()) {
                                    return operate;
                                }

                                return full.url;
                            }
                        },
                        {
                            "targets": [4],
                            "render": function (data, type, full) {
                                var d = new Date(full.createTime).Format($.i18n.prop('APP_DATE_FORMAT_FULL'));
                                return d;
                            }
                        },
                        {
                            "targets": [5],
                            "render": function (data, type, full) {
                                var status = '';
                                if (full.status == 'EXPORTING') {
                                    status = '<span class="label label-warning"><i class="fa fa-refresh fa-spin animated"></i> ' + $.i18n.prop('SPC_EXPORTFILE_STATUS_EXPORTING') + '</span>';
                                } else if (full.status == 'FINISHED') {
                                    status = '<span class="label label-success">' + $.i18n.prop('SPC_EXPORTFILE_STATUS_FINISHED') + '</span>';
                                } else if (full.status == 'FAILED') {
                                    status = '<span class="label label-danger">' + $.i18n.prop('SPC_EXPORTFILE_STATUS_FAILED') + '</span>';
                                }

                                return status;
                            }
                        },
                        {
                            "targets": [7],
                            "render": function (data, type, full) {
                                var operate = '<div class="center"><a href="javascript:void(0);" data-code="' + full.id + '">' + $.i18n.prop("GLOBAL_DELETE") + '</a></div>';
                                if (full.createBy == global.getCurrentUserName()) {
                                    return operate;
                                } else if (global.isAdmin()) {
                                    return operate;
                                }
                                return ""
                            }
                        }
                    ],
                    "initComplete": function (settings, json) {
                    }
                });
                components.tb_exportHistory.DataTable().on('draw.dt', function () {
                    var dt = $(this).dataTable();
                    components.tb_exportHistory.dataTable().api().column(0, {draw: 'applied'}).nodes().each(function (cell, i) {
                        var pageNum = dt.api().page();
                        var pageSize = dt.api().page.len();
                        cell.innerHTML = pageNum * pageSize + i + 1;
                    });

                    components.tb_exportHistory.dataTable().api().column(7, {draw: 'applied'}).nodes().each(function (cell, i) {
                        $(cell).find('a').on('click', function (e) {
                            e.preventDefault();
                            that.confirmDeleteExportFile($(cell).find('a').attr('data-code'));
                        })
                    });
                });
            },
            loadHistoryTable: function () {
                components.tb_exportHistory.DataTable().ajax.url('exportfile/list').load();
            },
            confirmDeleteExportFile: function (id) {
                $.smallBox({
                    title: $.i18n.prop('MESSAGEBOX_CONFIRM_TITLE'),
                    content: $.i18n.prop('SPC_EXPORTFILE_DELETE_CONFIRM') + "  <p class='text-align-right'>" +
                    "<a href='javascript:void(0);' data-code='"+id+"' class='btn btn-primary btn-sm'>" + $.i18n.prop("GLOBAL_BUTTON_YES") + "</a> " +
                    "<a href='javascript:void(0);' class='btn btn-danger btn-sm'>" + $.i18n.prop("GLOBAL_BUTTON_NO") + "</a></p>",
                    color: "#296191",
                    timeout: 5000,
                    icon: "fa fa-bell swing animated"
                });
                $('#divSmallBoxes').find('a[data-code='+ id +']').on('click', function (e) {
                    e.preventDefault();
                    that.doDeleteExportFile(id);
                });
            },
            doDeleteExportFile: function (id) {
                var param = {
                    id : id
                };
                exportM.deleteExportFile(param, function () {
                    msgBox.show({
                        content: $.i18n.prop('SPC_EXPORTFILE_DELETE_SUCCESS'),
                        type:'success'

                    });
                    components.tb_exportHistory.dataTable().api().ajax.reload();
                });
            }

        }
    }
);
